﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneralDFS.Common;
using GeneralDFS.DataEntity.Nodes;

namespace GeneralDFS.BusinessLogical.Chord
{
    public class FingerTable
    {
        public UInt64[] StartValues { set; get; }
        public NameNodeInfo[] Successors { set; get; }

        public FingerTable(NameNodeInfo localNode)
        {
            StartValues = new UInt64[64];
            Successors = new NameNodeInfo[64];
            for (var i = 0; i < Length; i++)
            {
                StartValues[i] = (localNode.ID + (UInt64)Math.Pow(2, i)) % UInt64.MaxValue;
                Successors[i] = localNode;
            }
        }

        public int Length
        {
            get { return StartValues.Length; }
        }

        public void DumpTable()
        {
            LogUtil.LogInfo("=====Table dump start======");
            for (var i = 0; i < StartValues.Length; i++)
            {
                LogUtil.LogInfo(StartValues[i]+"|"+Successors[i].ID+"|"+Successors[i].ServerName);
            }
            LogUtil.LogInfo("=====End dump table=======");
        }
    }
}